package leetcode;

/**
 * @author caifangyi
 * @date 2022/8/17
 */

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * 1302. 层数最深叶子节点的和
 *
 * 给你一棵二叉树的根节点 root ，请你返回 层数最深的叶子节点的和 。
 *
 * 
 *
 * 示例 1：
 *
 *
 *
 * 输入：root = [1,2,3,4,5,null,6,7,null,null,null,null,8]
 * 输出：15
 * 示例 2：
 *
 * 输入：root = [6,7,8,2,7,1,3,9,null,1,4,null,null,null,5]
 * 输出：19
 * 
 *
 * 提示：
 *
 * 树中节点数目在范围 [1, 104]之间。
 * 1 <= Node.val <= 100
 *
 * 来源：力扣（LeetCode）
 * 链接：https://leetcode.cn/problems/deepest-leaves-sum
 * 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
 */
public class Day1302 {

    public static void main(String[] args) {

    }

    /**
     * Definition for a binary tree node.
     */
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    class Solution {
        public int deepestLeavesSum(TreeNode root) {

            Deque<TreeNode> deque = new ArrayDeque<>();
            deque.push(root);

            while(!deque.isEmpty()){
                int sum = 0;
                Deque<TreeNode> deque2 = new ArrayDeque<>();
                while(!deque.isEmpty()){
                    TreeNode node = deque.pop();
                    sum += node.val;
                    if(node.left !=null){
                        deque2.push(node.left);
                    }
                    if(node.right !=null){
                        deque2.push(node.right);
                    }
                }
                if(deque2.isEmpty()){
                    return sum;
                }else{
                    deque = deque2;
                }
            }
            return 0;
        }
    }

}
